//
//  Graphics2DPrimitive.c
//  OpenGL
//
//  Created by xiaoxi on 2020/5/26.
//  Copyright © 2020 xiaoxi. All rights reserved.
//
#include "Graphics2DPrimitive.h"

GeometryGenerator Circle(const VEC3& p_pos,const float p_radius,const float p_numVertics,const COLOR& p_color)
{
    GeometryGenerator primitive;
    float t = 0.0f;
    for(int i=0;i<=p_numVertics;++i)
    {
        Vertex vertex;
        vertex.position = {p_pos.x + p_radius * cos(t),p_pos.y + p_radius * sin(t),p_pos.z};
        vertex.color = p_color;
        t += 2 * PI / p_numVertics;
        primitive.vertex.push_back(vertex);
        primitive.index.push_back(i);
    }
    return primitive;
}
GeometryGenerator FilledCircle(const VEC3& p_pos,const float p_radius,const float p_numVertics,const COLOR& p_color)
{
    GeometryGenerator primitive;
    float t = 0.0f;
    Vertex center;
    center.position = p_pos;
    center.color = p_color;
    primitive.vertex.push_back(center);
    for(int i=0;i<p_numVertics;++i)    //添加顶点
    {
        Vertex vertex;
        vertex.position = {p_pos.x + p_radius * cos(t), p_pos.y + p_radius * sin(t), p_pos.z};
        vertex.color =p_color;
        primitive.vertex.push_back(vertex);
        t += 2 * PI / p_numVertics;
    }
    int id = 1;
    int index = 0;
    for(;index<p_numVertics;index++)    //添加顶点索引
    {
        primitive.index.push_back(id+1);
        primitive.index.push_back(0);
        primitive.index.push_back(id);
        id++;
    }
    primitive.index.push_back(1);
    primitive.index.push_back(0);
    primitive.index.push_back(index);
    return primitive;
}
GeometryGenerator CircularAnnulus(const VEC3& p_pos,const float p_minRadius,const float p_maxRadius,const float p_numVertics,const COLOR& p_color)
{
    GeometryGenerator primitive;
    float t = 0.0f;
    for(int i=0;i<=p_numVertics;++i)
    {
        Vertex vertex;
        vertex.position = {p_pos.x + p_minRadius * cos(t),p_pos.y + p_minRadius * sin(t),p_pos.z};
        vertex.color = p_color;
        primitive.vertex.push_back(vertex);
        vertex.position = {p_pos.x + p_maxRadius * cos(t),p_pos.y + p_maxRadius * sin(t),p_pos.z};
        vertex.color = p_color;
        primitive.vertex.push_back(vertex);
        t += 2 * PI / p_numVertics;
    }
    int id = 1;
    int index = 0;
    for(;index<2*p_numVertics;index++)    //添加顶点索引
    {
        primitive.index.push_back(id-1);
        primitive.index.push_back(id);
        primitive.index.push_back(id+1);
        id++;
    }
    return primitive;
}
GeometryGenerator Ellipse(const VEC3& p_pos,const float p_high,const float p_short,const float p_numVertics,const COLOR& p_color)
{
    GeometryGenerator primitive;
    float t = 0.0f;
    Vertex center;
    center.position = p_pos;
    center.color = p_color;
    primitive.vertex.push_back(center);
    for(int i=0;i<p_numVertics;++i)    //添加顶点
    {
        Vertex vertex;
        vertex.position = {p_pos.x + p_high * cos(t), p_pos.y + p_short * sin(t), p_pos.z};
        vertex.color =p_color;
        primitive.vertex.push_back(vertex);
        t += 2 * PI / p_numVertics;
    }
    int id = 1;
    int index = 0;
    for(;index<p_numVertics;index++)    //添加顶点索引
    {
        primitive.index.push_back(id+1);
        primitive.index.push_back(0);
        primitive.index.push_back(id);
        id++;
    }
    primitive.index.push_back(1);
    primitive.index.push_back(0);
    primitive.index.push_back(index);
    return primitive;
}
GeometryGenerator Rectangular(const float p_zaxis,const float p_buttom,const float p_left,const float p_top,const float p_right,const COLOR& p_color)
{
    GeometryGenerator primitive;
    Vertex vertex;
    //添加顶点
    vertex.position = VEC3(p_left,p_buttom,p_zaxis);
    vertex.color = p_color;
    primitive.vertex.push_back(vertex);
    vertex.position = VEC3(p_right,p_buttom,p_zaxis);
    vertex.color = p_color;
    primitive.vertex.push_back(vertex);
    vertex.position = VEC3(p_right,p_top,p_zaxis);
    vertex.color = p_color;
    primitive.vertex.push_back(vertex);
    vertex.position = VEC3(p_left,p_top,p_zaxis);
    vertex.color = p_color;
    primitive.vertex.push_back(vertex);
    //添加顶点索引
    primitive.index.push_back(3);
    primitive.index.push_back(0);
    primitive.index.push_back(1);
    primitive.index.push_back(1);
    primitive.index.push_back(2);
    primitive.index.push_back(3);
    return primitive;
}
GeometryGenerator Rectangular(const RECT& p_rect,const COLOR& p_color)
{
    return Rectangular(p_rect.z_axis,p_rect.buttom,p_rect.left,p_rect.top,p_rect.right,p_color);
}
